#include <stdio.h>
#include "time_trigger.h"

//【注意:】以下状态链表可根据用户需要进行修改添加运行状态	
//LED运行状态：	
//以下状态是基于10ms执行一次LedHanleAll函数
//运行方式：亮0.1秒，灭0.1秒
const TimeTriggerState_t StateList1[] = 
{
	{STATE_ON,  10},				//10ms执行一次，执行10次为100ms，0.1s
	{STATE_OFF, 10},
};
//运行方式：亮1秒，灭1秒
const TimeTriggerState_t StateList2[] = 
{
	{STATE_ON, 50},				//10ms执行一次，执行50次为500ms，0.5s
	{STATE_OFF, 50},
};
//运行方式：快闪到慢闪
const TimeTriggerState_t StateList3[] = 
{
	{STATE_ON, 10},
	{STATE_OFF, 10},
	{STATE_ON, 20},
	{STATE_OFF, 20},
	{STATE_ON, 40},
	{STATE_OFF, 40},
	{STATE_ON, 80},
	{STATE_OFF, 80},
};


const TimeTriggerState_t StateList4[] = 
{
	{STATE_ON,	50},
	{STATE_OFF,	60},
	{STATE_ON,	15},
};
/*
	添加示例：
	//运行方式：亮0.1秒，灭1秒，亮1秒，灭0.1秒
	const TimeTriggerState_t StateList4[] = 
	{
		{STATE_ON, 10},
		{STATE_OFF, 100},
		{STATE_ON, 100},
		{STATE_OFF, 10},
	};
*/
//Gpio 类型
typedef enum
{
		LED_R     = 0,
        LED_G     = 1,
        LED_B     = 2,
        PIN_MAX
}OutputPinType_t;

//根据当前输出对象数量创建结构体数组
struct TimeTriggerIf_t OutputPin[PIN_MAX];

//LED使能函数
void OutputPinEnable(OutputPinType_t type)
{
	OutputPin[type].param.ctrlcmd = 1;
}
//LED禁止函数
void OutputPinDisable(OutputPinType_t type)
{
	OutputPin[type].param.ctrlcmd = 0;
}
//单次触发多次执行，执行次数设置
void OutputPinRunNumByOneShotMultipleMode(OutputPinType_t type, int num)
{
	if(OutputPin[type].param.ctrtype != OneShotMultipleMode)	return;
	
	if((num <= 0) || (num > 64))		return;
	
	OutputPin[type].param.runnum = num;
}

//当前输出状态
unsigned char OutputPinStatus(OutputPinType_t type)
{
	return(OutputPin[type].param.ctrlcmd);
	//注意：此处返回的状态为软件状态
}

/**************************************************LED控制函数*****************************************************/
//【注意】LED控制函数接口要遵循此模式 void led_driver(unsigned char led_state)

void LED_R_Driver(unsigned char led_state)
{
	printf(" LED_R Leval = %d \n", led_state);
}

void LED_G_Driver(unsigned char led_state)
{
	printf(" LED_G Leval = %d \n", led_state);
}

void LED_B_Driver(unsigned char led_state)
{
	printf(" LED_B Leval = %d \n", led_state);
}

/******************************************************************************************************************
	输出引脚应用区域
******************************************************************************************************************/

struct TimeTriggerIf_t *pList;

void OutputPinAppInit(void)
{
	//受控单次触发多次执行
	TimeTriggerAdd(&pList, &OutputPin[LED_R],  StateList2, (sizeof(StateList2)/sizeof(TimeTriggerState_t)) ,	CycleMode, LED_R_Driver);	
	//受控单次触发多次执行，执行次数设置，例如以下设置为3，即为触发一次 闪烁3次
	OutputPinRunNumByOneShotMultipleMode(LED_R, 3);
	
	//受控单次触发模式
	TimeTriggerAdd(&pList, &OutputPin[LED_G],  StateList2, (sizeof(StateList2)/sizeof(TimeTriggerState_t)) ,	CycleMode, LED_G_Driver);	
	
	//循环触发模式
	TimeTriggerAdd(&pList, &OutputPin[LED_B],  StateList2, (sizeof(StateList2)/sizeof(TimeTriggerState_t)) ,	CycleMode, LED_B_Driver);	
	
}



int main(int argc, char** argv) 
{
	int i;
	
	//输出引脚初始化
	OutputPinAppInit();
	
	OutputPinEnable(LED_R);		//受控单次触发多次执行
	OutputPinEnable(LED_G);		//受控单次触发模式
	OutputPinEnable(LED_B);		//循环触发模式
	
	for(i = 0; i < 1024; i++)
	{	
		TimeTriggerHanleAll(&pList);
		
		printf("      %dms \n", i*10);
		
		if(i == 128)
		{
			TimeTriggerDel(&pList,&OutputPin[LED_R]);
			TimeTriggerDel(&pList,&OutputPin[LED_G]);
			TimeTriggerDel(&pList,&OutputPin[LED_B]);
		}
	}
	return 0;
}




